<?php

include_once("interface.php");

class Save extends Data
{
    public $fn = "/tmp/config.json";
    public $title = "Upload Configuration";
    public $module = "save";
    public $data;
    private $module_list = array();

    public function __construct() {
    }

    //read the data from files and return a complete object
    function readDataFromSystem() {
        return $this->data;
    }

    //write the obj to the file
    function commitDataToSystem($obj) {
            /*
            $str = file_get_contents($this->fn);
            $jsonstr = preg_replace('/  |\r|\n/', '', $str);
            error_log("save commit $jsonstr \n");
            $obj = json_decode($jsonstr, true);
            foreach ($obj as $key => $value) {
                error_log("$key => $value\n");
                switch($key) {
                    case "route":
                        include_once("route.php");
                        $o = new StaticRoutes();
                        $o->commitDataToSystem($value);
                        break;
                    case "dns":
                        include_once("dns.php");
                        $o = new DNS();
                        $o->commitDataToSystem($value);
                        break;
                    default:
                        break;
                }
            }
            */
        if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $this->fn)) {
            $jsonstr = file_get_contents($this->fn);

            //validate json
            $obj = json_decode($jsonstr, true);
            if($obj == null) {
                return "\"Invalid configuration file, please upload a valid json file\"";;
            }

            foreach ($obj as $m => $value) {
                try {
                    $o = ModuleFactory::createModule($m);
                    $ret = $o->commitDataToSystem($value);
                    $retstr = $retstr."<br>".$ret;
                }
                catch (Exception $e) {
                    echo 'Caught exception: ',  $e->getMessage(), "\n";
                }
            }
            $jsonstr = json_encode($retstr);
            return $jsonstr;
        } else {
            error_log("Save commit failed\n");
            return "error";
        }
    }

    function printModuleHTML() {
$str = <<<END
    <br>
    <fieldset class="ui-widget-content">
    <legend><b>Upload Configuration</b></legend>
        <form name="form" action="" method="POST" enctype="multipart/form-data">
            Please select a file and click Upload button<br>
            <input id="uploadfile" type="file" size="45" name="uploadfile"><br>
            <button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">Upload</button>
        </form>
        <br>
        <span id="loading" style="display:none;">Loading the configuration ... </span>
        <span id="status" ></span>
    </fieldset>
    <br>
END;
        return $str;
    }

    function printModuleScript() {
$str = <<<END
<script type="text/javascript" src="../js/ajaxfileupload.js" ></script>
<script type="text/javascript">
    function ajaxFileUpload()
    {
        $("#loading")
        .ajaxStart(function(){
            $(this).show();
        })
        .ajaxComplete(function(){
            $(this).hide();
        });

        $.ajaxFileUpload
        ({
            url: 'index.php?module=save&m=commit',
            fileElementId:'uploadfile',
            dataType: 'json',
            success: function (data, status)
            {
                $('#status').html(data);
            },
            error: function (data, response, e)
            {
                $('#status').html(data);
            }
        })
        return false;
    }

function writeDataToHTML(data) {
}

</script>

END;
        return $str;
    }
}

?>
